package grails.coherence.enhancer.comparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Comparator;

import com.tangosol.io.ExternalizableLite;
import com.tangosol.util.ExternalizableHelper;

class GroovyComparator implements Comparator, ExternalizableLite {
	
	String propertyName
	String order	
	
	int compare(a, b) {		
		(order == 'asc' ? 1 : -1) * (a."$propertyName" <=> b."$propertyName")		
	}
		
	void readExternal(DataInput input) throws IOException {
		propertyName = ExternalizableHelper.readSafeUTF(input)
		order = ExternalizableHelper.readSafeUTF(input)	
	}
	
	void writeExternal(DataOutput output) throws IOException {
		ExternalizableHelper.writeSafeUTF(output, propertyName)
		ExternalizableHelper.writeSafeUTF(output, order)
	}	
}
